package com.grgbanking.it.project.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.grgbanking.it.common.HttpConnection;
import com.grgbanking.it.common.Message;
import com.grgbanking.it.project.entity.OperationWork;
import com.grgbanking.it.system.entity.User;
import com.grgbanking.it.wx.common.creator.WXBaseMessageCreator;
import com.grgbanking.it.wx.entity.WXMessage;
import com.grgbanking.it.wx.entity.WXPassiveMessage;
import com.grgbanking.it.wx.entity.WXTextMessage;

/**
 * 运维信息工作处理
 * 
 * WXOperationWorkMessageCreator
 * 
 * @author 潘广伟
 * @Email p_3er@qq.com
 * @Date 2015-1-7 下午5:36:38
 * 
 * @version 1.0.0
 *
 */
@Component
public class WXOperationWorkMessageCreator extends WXBaseMessageCreator{
	@Autowired
	protected HttpConnection httpConnection;
	protected Integer agentId = WXProjectConstant.REPAIR_AGENT_ID;
	public WXOperationWorkMessageCreator(){
		super.agentId = agentId;
		super.httpConnection = httpConnection;
	}
	
	/**
	 * 菜单触发工作创建时发送
	 * getWhenWorkFromMenu
	 * 
	 * @param username 
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public String getWhenWorkFromMenu(String username,String timestamp,String nonce) throws Exception{
		WXPassiveMessage textMessage = createPassiveMessage();
		textMessage.setToUserName(username);
		textMessage.setContent(Message.WORK_CREATED);
		String xml = textMessageToXml(textMessage);
		xml = encryptMsg(xml, timestamp, nonce);
		return xml;
	}
	
	/**
	 * 评价反馈工作后发送
	 * getWhenWorkFeedback
	 * 
	 * @param username
	 * @param timestamp
	 * @param nonce
	 * @param isSuccess
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public String getWhenWorkFeedback(String username,String timestamp,String nonce,boolean isSuccess) throws Exception{
		WXPassiveMessage textMessage = createPassiveMessage();
		textMessage.setToUserName(username);
		if(isSuccess){
			textMessage.setContent(Message.WORK_FEEDBACK_SUCCESS);
		}else{
			textMessage.setContent(Message.WORK_FEEDBACK_FAULT);
		}
		
		String xml = textMessageToXml(textMessage);
		xml = encryptMsg(xml, timestamp, nonce);
		return xml;
	}
	
	/**
	 * 为待分配的工作发送信息
	 * sentForDistribute
	 * 
	 * @param username
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public void sentForDistribute(String username) throws Exception{
		WXMessage message = createMessage();
		message.setTouser(username);
		message.setMsgtype("text");
		message.setText(new WXTextMessage(Message.WORK_CREATED_TO_DISTRIBUTE));
		sendJSONMessage(message);
	}
	
	/**
	 * 二维码中工作创建时发送
	 * sentWhenWorkCreated
	 * 
	 * @param username 
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public void sentWhenWorkFromCode(String username) throws Exception{
		WXMessage message = createMessage();
		message.setTouser(username);
		message.setMsgtype("text");
		message.setText(new WXTextMessage(Message.WORK_CREATED));
		sendJSONMessage(message);
	}
	
	/**
	 * 工作分配后发送给工程师
	 * sentWhenWorkDistribute
	 * 
	 * @param engineer
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public void sentToEngineerWhenWorkDistribute(String engineer,OperationWork work) throws Exception{
		WXMessage message = createMessage();
		message.setTouser(engineer);
		message.setMsgtype("text");
		message.setText(new WXTextMessage(Message.getDistributeMessageToEngineer(work)));
		sendJSONMessage(message);
	}
	
	/**
	 * 工作分配后发送给申请用户
	 * sentWhenWorkDistribute
	 * 
	 * @param engineer
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public void sentToUserWhenWorkDistribute(String user,User engineer) throws Exception{
		WXMessage message = createMessage();
		message.setTouser(user);
		message.setMsgtype("text");
		message.setText(new WXTextMessage(Message.getDistributeMessageToUser(engineer)));
		sendJSONMessage(message);
	}
	/**
	 * 工作完成后发送给申请用户
	 * sentWhenWorkDistribute
	 * 
	 * @param user
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public void sentToUserWhenWorkFinish(String user) throws Exception{
		WXMessage message = createMessage();
		message.setTouser(user);
		message.setMsgtype("text");
		message.setText(new WXTextMessage(Message.WX_WORK_FINISHED));
		sendJSONMessage(message);
	}
	
	/**
	 * 工作挂起后发送给申请用户
	 * sentToUserWhenWorkHangUp
	 * 
	 * @param user
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public void sentToUserWhenWorkHangUp(String user,User engineer) throws Exception{
		WXMessage message = createMessage();
		message.setTouser(user);
		message.setMsgtype("text");
		message.setText(new WXTextMessage(Message.getHangUpMessageToUser(engineer)));
		sendJSONMessage(message);
	}
	
	/**
	 * 工作转移后发送给申请用户
	 * sentToUserWhenWorkShift
	 * 
	 * @param user
	 * @param engineer
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public void sentToUserWhenWorkShift(String user,User engineer) throws Exception{
		WXMessage message = createMessage();
		message.setTouser(user);
		message.setMsgtype("text");
		message.setText(new WXTextMessage(Message.getShiftMessageToUser(engineer)));
		sendJSONMessage(message);
	}

	


	
}
